package homework1.client;

import homework1.common.Message;
import homework2.common.CommandType;

import java.io.*;
import java.net.Socket;
import java.util.Scanner;

/**
 * 客户端业务处理类
 *
 * @author jian
 * @date 2024/01/08 19:09
 **/
public class Client {
    String id;

    Client(String id){
        this.id = id;
    }
    public void  start(){
        new Thread(new Runnable() {

            @Override

            public void run() {
                Scanner sc = new Scanner(System.in);
                boolean loop = true;
                try {
                    Socket socket = new Socket(Properties.ADDRESS, Properties.PORT);
                    System.out.println("客户端"+id+" 连接成功，输入文件名以发送文件");
                    ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
                    Message idMessage = new Message();
                    idMessage.setUserId(id);
                    idMessage.setMesType(CommandType.USER_LIST);
                    oos.writeObject(idMessage);
                    while (loop) {
                        operate(socket);
                    }

                } catch (IOException e) {
                    System.out.println("发送客户端id出错");;
                } catch (ClassNotFoundException e) {
                    throw new RuntimeException(e);
                }

            }

        }).start();
    }

    public static void operate(Socket socket) throws IOException, ClassNotFoundException {
        boolean loop = true;
        while (loop) {
            ClientService.sendJsonOrXml(socket);
        }
    }

}
